﻿using System;
using System.IO;
using System.Drawing;
using System.Collections.ObjectModel;
using System.Collections.Generic;

using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Graphics.Sprites;
using SdlDotNet.Input;


namespace SDLTest
{
    class Escenario:IDisposable
    {
        private int width = 640; //screen width
        private int height = 480; //screen height
        private Surface screen;
        private Surface background;
        private SpriteCollection master = new SpriteCollection();
        private Collection<Rectangle> rects = new Collection<Rectangle>();
        
        public Escenario() {
            Video.WindowIcon();
            Video.WindowCaption = "SDL.NET - Bounce Sprites";
            screen = Video.SetVideoMode(width, height);
            background = new Surface("dibujos/fondo.png");
            screen.Blit(background);
            screen.Update();
            SurfaceCollection sc = new SurfaceCollection();
            Surface s = new Surface("dibujos/platillo.png");
            sc.Add(s);
            master.Add(new PSprite(sc,new Point(0,0)));
            master.EnableTickEvent();
            Events.Tick += new EventHandler<TickEventArgs>(this.Tick);
            Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(this.TeclaPulsada);
            Events.Run();

        }
        private void TeclaPulsada(object sender, KeyboardEventArgs args) {
            Console.WriteLine(args.Key);
            if (args.Key == Key.UpArrow)
            {
                ((PSprite)master[0]).UpArrow();
            }
            if (args.Key == Key.LeftArrow) {
                ((PSprite)master[0]).LeftArrow();
            }
            if (args.Key == Key.RightArrow)
            {
                ((PSprite)master[0]).RightArrow();
            }
            if (args.Key == Key.DownArrow)
            {
                ((PSprite)master[0]).DownArrow();
            }
            if (args.Key == Key.Space) {
                SurfaceCollection sc = new SurfaceCollection();
                Surface s = new Surface("dibujos/proyectil.png");
                sc.Add(s);
                PSprite p = new PSprite(sc, new Point(((PSprite)master[0]).X, ((PSprite)master[0]).Y));
                p.SetVx((int)(((PSprite)master[0]).GetVx() * 2));
                p.SetVy((int)(((PSprite)master[0]).GetVy() * 2));
                master.Add(p);
            }
        }
        private void Tick(object sender, TickEventArgs args)
        {
            
            screen.Blit(master);
            
            
            screen.Update();
            screen.Erase(master, background);
            
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
        private void KeyboardDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Key.Escape || e.Key == Key.Q)
            {
                Events.QuitApplication();
            }
        }
        
    }
}
